//
//  FCExchangeRatesVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/6.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCExchangeRatesVC.h"
#import "XLSlideMenu.h"
@interface FCExchangeRatesVC ()<UITableViewDelegate,UITableViewDataSource>{
    UIView     *_viewNav;
    
}
/* tableView */
@property (strong, nonatomic) UITableView *tableView;

/* shadowView */
@property (strong, nonatomic) UIView *shadowView;
/* title */
@property (strong, nonatomic) NSArray  *titleArr;
/* fulltitle */
@property (strong, nonatomic) NSArray  *fullTitleArr;
/* rates */
@property (strong, nonatomic) NSArray  *rateArr;

/* 数据源 */
@property (strong, nonatomic) NSMutableArray *allRateArr;

/* <#Annotation#> */
@property (strong, nonatomic) FCBalanceModel *banlanceModel;
/*头部一些UI*/
/* otherLabel */
@property (strong, nonatomic) UILabel *otherLabel;
/* fullNameLabel */
@property (strong, nonatomic)  UILabel *fullNameLabel;
/* fullNameLabel */
@property (strong, nonatomic)  UILabel *usLabel;
/* fullNameLabel */
@property (strong, nonatomic)  UILabel *usFullNameLabel;
/* fullNameLabel */
@property (strong, nonatomic)  UILabel *usCountLabel;

/* walletid */
@property (copy, nonatomic) NSString *walletID;


@end

@implementation FCExchangeRatesVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [FCLocalUserHelper saveIsMainVC:@"0"];
    self.view.backgroundColor = kWhiteColor;
    [self setNav];
    [self setupUI];
    [self loadData];
}
-(void)loadData{
    
    self.titleArr = @[@"AED",@"AFD",@"ALL",@"AUD",@"BDT",@"CAD",@"CNY"];
    self.fullTitleArr = @[@"AEDasda",@"AFDqdq",@"ALLadsad",@"AUDqwedqew",@"BDTqeq",@"CAqeqD",@"CNYweqqewq"];
    self.rateArr = @[@"123132",@"23243",@"12313",@"3132312",@"31321",@"31321",@"3121321321"];
    
    NSString *walletId = [FCLocalUserHelper getDidScrollWallId];
    //记录一下id
    self.walletID = walletId;
    
    NSLog(@"主页移动时的walletId:%@",walletId);
    FCBalanceModel *banlanceModel = [FCLocalUserHelper getFMDBBalanceData:walletId];
    self.banlanceModel = banlanceModel;
    NSArray *arr = banlanceModel.currentResult;
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FCCurrentResultModel *model = [FCCurrentResultModel mj_objectWithKeyValues:obj];
        
        [self.allRateArr addObject:model];
        
    }];
    
    FCExchangeRatesModel *rateModel = [FCLocalUserHelper getFMDBExchangeRatesModel:self.walletID];
    
    if ([rateModel.walletId isEqualToString:self.walletID]) {
        
        NSIndexPath *index = [NSIndexPath indexPathForRow:rateModel.index inSection:0];
        [self.tableView selectRowAtIndexPath:index animated:NO scrollPosition:(UITableViewScrollPositionNone)];
        
        FCCurrentResultModel *model = self.allRateArr[rateModel.index];
        self.usLabel.text = model.currentName;
        self.usFullNameLabel.text = model.fullName;
        self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
        
    }else{
        
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView selectRowAtIndexPath:index animated:NO scrollPosition:(UITableViewScrollPositionNone)];
        FCCurrentResultModel *model = self.allRateArr[0];
        self.usLabel.text = model.currentName;
        self.usFullNameLabel.text = model.fullName;
        self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FCCurrentResultModel *model;
    switch (indexPath.row) {
        case 0:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
           
        }
            break;
        case 1:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 2:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
           self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 3:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 4:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 5:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 6:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
           self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 7:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 8:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
        case 9:
        {
            model = self.allRateArr[indexPath.row];
            self.usLabel.text = model.currentName;
            self.usFullNameLabel.text = model.fullName;
            self.usCountLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
            
        }
            break;
            
        default:
            break;
    }
    
    model = self.allRateArr[indexPath.row];
    FCExchangeRatesModel *rateModel = [FCExchangeRatesModel new];
    rateModel.walletId = self.walletID;
    rateModel.index = indexPath.row;
    rateModel.XID = [FCLocalUserHelper getXidStr];
    rateModel.price = model.price;
    rateModel.currentName = model.currentName;
    [FCLocalUserHelper saveFMDBExchangeRatesModel:rateModel];
   
    NSLog(@"下标是:%zd",indexPath.row);
    model.walletId = self.walletID;
    model.XID = [FCLocalUserHelper getXidStr];
    [FCLocalUserHelper saveFMDBCurrentResultModel:model];
    //发送点击改变不同等值货币的通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeDifferentEquivalentCoins" object:model];
    
    
}
#pragma mark-UITableViewDelegate&UITableViewDataSource

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120*KScreenHeightScale1334;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allRateArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"exchangeRateCell"];
    cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"exchangeRateCell"];
    FCCurrentResultModel *model = self.allRateArr[indexPath.row];
    
    //自定义cell选中背景颜色
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = HEXCOLOR(0xf2f6fa);
    //币种
    UILabel *nameLabel = [UILabel new];
    [cell addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.centerY.mas_equalTo(-20*KScreenHeightScale1334);
    }];
    nameLabel.font = Kfont(32);
    nameLabel.text = model.currentName;
    //货币全称
    UILabel *fullNameLabel = [UILabel new];
    [cell addSubview:fullNameLabel];
    [fullNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.centerY.mas_equalTo(20*KScreenHeightScale1334);
    }];
    fullNameLabel.font = Kfont(28);
   
    fullNameLabel.textColor = kGrayColor;
    fullNameLabel.text = model.fullName;
    //货币汇率
    UILabel *rateLabel = [UILabel new];
    [cell addSubview:rateLabel];
    [rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30*KScreenWidthScale750);
        make.centerY.mas_equalTo(0);
    }];
    rateLabel.font = Kfont(32);
    
    rateLabel.textColor = kBlackColor;
    rateLabel.text = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]];
    
    
    
    return cell;
}


#pragma mark-设置UI
-(void)setupUI{
    
    UIView *shadowView = [UIView new];
    self.shadowView = shadowView;
    [self.view addSubview:shadowView];
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewNav.mas_bottom).offset(30*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.height.mas_equalTo(328*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    //阴影的颜色
    shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
    //阴影的透明度
    shadowView.layer.shadowOpacity = 0.3f;
    //阴影的圆角
    shadowView.layer.shadowRadius = 4.f;
    //阴影偏移量
    shadowView.layer.shadowOffset = CGSizeMake(0,4);
    
    //topimageView
    UIImageView *topImageview = [UIImageView new];
    [shadowView addSubview:topImageview];
    [topImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    topImageview.image = [UIImage imageNamed:@"exchange_fee"];
    //midImageView
    UIImageView *midImageView = [UIImageView new];
    [topImageview addSubview:midImageView];
    [midImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40*KScreenHeightScale1334);
        make.centerX.mas_equalTo(0);
    }];
    midImageView.image = [UIImage imageNamed:@"transfer_chat"];
    //分割线
    UIView *viewLine =[UIView new];
    [topImageview insertSubview:viewLine belowSubview:midImageView];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(328*KScreenHeightScale1334);
    }];
    viewLine.backgroundColor =HEXCOLOR(0x00568c);
    
    //btc
    UILabel *otherLabel = [UILabel new];
    self.otherLabel = otherLabel;
    [topImageview addSubview:otherLabel];
    [otherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(midImageView);
        make.left.mas_equalTo(40*KScreenWidthScale750);
    }];
//    otherLabel.text = @"BTC";
    otherLabel.font = Kfont(40);
    otherLabel.textColor = kWhiteColor;
    //全称
    UILabel *fullNameLabel = [UILabel new];
    self.fullNameLabel = fullNameLabel;
    [topImageview addSubview:fullNameLabel];
    [fullNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(otherLabel.mas_bottom).offset(10*KScreenHeightScale1334);
        make.left.mas_equalTo(otherLabel);
        make.right.mas_equalTo(viewLine.mas_left);
    }];
//    fullNameLabel.text = @"Bitcoin";
    fullNameLabel.font = Kfont(32);
    fullNameLabel.textColor = HEXCOLOR(0x82b8d9);
    fullNameLabel.numberOfLines = 2;
    //我们的币种
    UILabel *usLabel = [UILabel new];
    self.usLabel = usLabel;
    [topImageview addSubview:usLabel];
    [usLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(midImageView);
        make.right.mas_equalTo(-40*KScreenWidthScale750);
    }];
//    usLabel.text = @"CNY";
    usLabel.font = Kfont(40);
    usLabel.textColor = kWhiteColor;
    //全称
    UILabel *usFullNameLabel = [UILabel new];
    self.usFullNameLabel = usFullNameLabel;
    [topImageview addSubview:usFullNameLabel];
    [usFullNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(otherLabel.mas_bottom).offset(10*KScreenHeightScale1334);
        make.right.mas_equalTo(usLabel);
    }];
//    usFullNameLabel.text = @"China Yuan";
    usFullNameLabel.font = Kfont(32);
    usFullNameLabel.textColor = HEXCOLOR(0x82b8d9);
    
    //otherCount
    UILabel *otherCountLabel = [UILabel new];
    [topImageview addSubview:otherCountLabel];
    [otherCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(fullNameLabel.mas_bottom).offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(otherLabel);
    }];
    otherCountLabel.text = @"1.00";
    otherCountLabel.font = Kfont(52);
    otherCountLabel.textColor = kWhiteColor;
    //我们的价值
    UILabel *usCountLabel = [UILabel new];
    self.usCountLabel = usCountLabel;
    [topImageview addSubview:usCountLabel];
    [usCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(usFullNameLabel.mas_bottom).offset(60*KScreenHeightScale1334);
        make.right.mas_equalTo(usLabel);
//        make.left.mas_equalTo(viewLine.mas_right);
    }];
//    usCountLabel.text = @"64,252.21";
    usCountLabel.font = Kfont(52);
    usCountLabel.textColor = kWhiteColor;
   
    dispatch_async(dispatch_get_main_queue(), ^{
       
        switch ([self.banlanceModel.walletId integerValue]) {
            case 1001:
            {
                self.otherLabel.text = @"BTC";
                self.fullNameLabel.text = Localized(@"比特币");
            }
                break;
            case 1005:
            {
                self.otherLabel.text = @"LTC";
                self.fullNameLabel.text = Localized(@"莱特币");
            }
                break;
            case 1006:
            {
                self.otherLabel.text = @"BCH";
                self.fullNameLabel.text = Localized(@"比特币现金");
            }
                break;
            case 1009:
            {
                self.otherLabel.text = @"CCH";
                self.fullNameLabel.text = Localized(@"加密现金币");
            }
                break;
            case 1010:
            {
                self.otherLabel.text = @"ZEC";
                self.fullNameLabel.text = Localized(@"零币");
            }
                break;
            default:
                break;
        }
    });
    
    [self tableView];
}
//懒加载tableView
-(UITableView*)tableView{
    
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView ];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.shadowView.mas_bottom).offset(20*KScreenHeightScale1334);
            make.left.mas_offset(0);
            make.right.mas_offset(0);
            make.bottom.mas_offset(0);
        }];
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.backgroundColor = kWhiteColor;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"汇率");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    
//    [self.navigationController popViewControllerAnimated:YES];
    [FCLocalUserHelper pushMainTabbarController];
}

-(NSMutableArray *)allRateArr{
    if (!_allRateArr) {
        _allRateArr = [NSMutableArray new];
    }
    return _allRateArr;
}

@end
